using System; 
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine; 

namespace XFGameFramework.Localization
{
    /// <summary>
    /// 多语言配表
    /// </summary>
    [CreateAssetMenu(menuName ="XFKT/Localization(多语言配置)")]
    public class MultiLanguageConfigs : ScriptableObject
    {

        #region 字段 

        /// <summary>
        /// 显示名称(仅用于编辑器显示,没有实际功能)
        /// </summary>
        [Tooltip("显示名称(仅用于编辑器显示,没有实际功能)")]
        [Header("显示名称(仅用于编辑器显示,没有实际功能)")]
        public string displayName = string.Empty;
         
        [Tooltip("多语言配置集合")]
        [Header("多语言配置集合")]
        [SerializeField]
        private List<LanguageConfig> languages = new List<LanguageConfig>();

        [HideInInspector]
        public string excel_path;

        private Dictionary<SystemLanguage, LanguageConfig> languages_dic = new Dictionary<SystemLanguage, LanguageConfig>();

#if UNITY_EDITOR 
        private float time;
#endif

#endregion


        #region 属性

        /// <summary>
        /// 当前配表中所有的语言配置
        /// </summary>
        public List<LanguageConfig> Languages 
        {
            get 
            { 
#if UNITY_EDITOR  
                if (languages.Count == 0 && Time.realtimeSinceStartup - time > 1) 
                { 
                    // 尝试查询
                    string asset_path = AssetDatabase.GetAssetPath(this);
                    string guid = AssetDatabase.AssetPathToGUID(asset_path);
                    string path = string.Format("Assets/Extensions/Localization/{0}", guid);

                    if (AssetDatabase.IsValidFolder(path)) 
                    {
                        string[] guids = AssetDatabase.FindAssets(string.Format("t:{0}", typeof(LanguageConfig).FullName), new string[] { path });

                        foreach (var item in guids)
                        {
                            string assetPath = AssetDatabase.GUIDToAssetPath(item);
                            LanguageConfig languageConfig = AssetDatabase.LoadAssetAtPath<LanguageConfig>(assetPath);
                            if (languageConfig == null) continue;
                            languages.Add(languageConfig);
                        }

                        if (languages.Count != 0)
                        {
                            EditorUtility.SetDirty(this);
                            AssetDatabase.SaveAssets();
                        }
                    }
                     
                    time = Time.realtimeSinceStartup;
                }

#endif

                return languages;
            }
        }
           
        #endregion


        /// <summary>
        /// 查询某一个语言的配置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public LanguageConfig GetLanguageConfigData(SystemLanguage language) 
        {
             
            if(languages_dic.Count == 0) 
            {
                foreach (var item in languages)
                {

                    if(item == null) continue;

                    if (languages_dic.ContainsKey(item.language))
                        throw new Exception(string.Format("语言:{0}重复!", language));
                    languages_dic.Add(item.language, item);
                }
            }

            if(languages_dic.ContainsKey(language))
                return languages_dic[language];
             
            return null;
        }
        
        /// <summary>
        /// 清理字典缓存(内部方法)
        /// </summary>
        public void ClearCache()
        { 
            languages_dic.Clear();
        }
  
    }

}
